<?php 

require 'functions.php';
require_once 'data/data.php';

/*$conn = mysqli_connect(`localhost:8088`,'root','123456');
    mysqli_select_db($conn,'demo');
    $sql = 'select id,email,user_name,(year(now())-year(birthday)) as age,birthday,gender,courses from `user`';
    $result = mysqli_query($conn, $sql);

    $data = [];
    while($row = mysqli_fetch_array($result,MYSQLI_ASSOC)) {
      $row['gender'] = $row['gender'] == 1 ?'男':'女';
      $data[] = $row;
    }
    mysqli_close($conn);*/

$data = @query('select id,email,user_name,(year(now())-year(birthday)) as age,birthday,gender,courses from user');

if ($_SERVER['REQUEST_METHOD'] === 'POST'){
  $searchkey = isset($_POST['search_button'])?trim(htmlspecialchars($_POST['search_key'])):'';
  $searchData = [];
  foreach ($data as $row){
    if (stripos($row['user_name'], $searchkey) !== false) {
      $searchData[] = $row;
    }else if ($searchkey == '') {
      $searchData[] = $row;
    }
  }
  $data = $searchData;
}


if(isset($_POST['sort_button'])){
  switch ($_POST['sort_key']) {
    case 0:
      usort($data,'compareById');
      break;
    case 1:
        usort($data,'compareByName');
        break;
    case 2:
        usort($data,'compareByAge');
        break;    
    default:
      # code...
      break;
  }
}
?>
<!DOCTYPE html>
<html>
<head>
  <meta charset="utf-8">
  <title></title>
  <style type="text/css">
  	body{}
  	#table{
  	  width: 800px;
  	  border: 1px solid #999;
  	  margin: 10px auto;
  	  text-align: center;
  	}
    a{
      color: #777;
      text-decoration: none;
    }
    a:hover {
      color: #9ce;
    }
    a.submit{
      display: inline-block;
      height: 45px;
      line-height: 45px;
      text-align: center;
      padding: 0px 18px;
      margin: 10px 0px;
      background: #cecece;
      color: #777;
      text-decoration: none;
      border-radius: 4px;
    }
    a.submit:hover {
      background: #dedede;
      color: #999;
    }
  </style>
</head>
<body>
  <form method="post">
  	<input type="text" name="search_key" placeholder="key word">
  	<input type="submit" name="search_button"><br/>
  	<select name="sort_key">
  		<option value="0">按id</option>
  		<option value="1">按name</option>
  		<option value="2">按age</option>
  	</select>
  	<input type="submit" name="sort_button"><br/>
  </form>
  <table id="table" border="1" cellspacing="0" cellpadding="2">
  	<tr>
  	  <th>id</th>
  	  <th>email</th>
  	  <th>use name</th>
  	  <th>age</th>
  	  <th>birth</th>
  	  <th>gender</th>
      <th>courses</th>
      <th>actions</th>
  	</tr>
    <?php foreach ($data as $row): ?>
    <tr>
      <td><?=$row['id'] ?></td>
      <td><?=$row['email'] ?></td>
      <td><?=$row['user_name'] ?></td>
      <td><?=$row['age'] ?></td>
      <td><?=$row['birthday'] ?></td>
      <td><?=$row['gender'] ?></td>
      <td><?=$row['courses'] ?></td>
      <td><a href="user_info.php?op=2&id=<?= $row['id'] ?>">编辑 </a><a href="delete.php?id=<?= $row['id'] ?>"> 删除</a></td>
    </tr>
    <?php endforeach; ?>
  </table>
  <div>
      <a class="submit" href="user_info.php?op=1">user add</a>
  </div>
</body>
</html>